<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/static/layui/css/layui.css}">
</head>
<body>

    <div class="demoTable">
        名字：
        <div class="layui-inline">
            <input class="layui-input" name="username" id="usernameId" autocomplete="off">
        </div>
        邮箱：
        <div class="layui-inline">
            <input class="layui-input" name="email" id="emailId" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="reload">搜索</button>
    </div>
    <table class="layui-hide" id="test" lay-filter="test"></table>

    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="deleteAll">批量删除</button>
            <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        </div>
    </script>

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script type="text/html" id="imageTpl">
        <img src="/pic/{{d.image}}"/>
    </script>


    <script th:src="@{/static/layui/layui.js}" charset="utf-8"></script>
    <script th:src="@{/static/jquery-2.1.4.js}" charset="utf-8"></script>
    <script th:src="@{/static/mylayer.js}" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

    <script>
        layui.use('table', function(){
            var table = layui.table;

            table.render({
                elem: '#test'
                ,url:'/user/list'
                ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                    title: '提示'
                    ,layEvent: 'LAYTABLE_TIPS'
                    ,icon: 'layui-icon-tips'
                }]
                ,title: '用户数据表'
                ,cols: [
                    [
                    {type: 'checkbox', fixed: 'left'}
                    ,{field:'id', title:'ID', fixed: 'left', sort: true}
                    ,{field:'username', title:'用户名'}
                    ,{field:'password', title:'密码'}
                    ,{field:'email', title:'邮箱'}
                    ,{field:'image', title:'头像',templet: '#imageTpl'}
                    ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
                ]
                ]
                ,page: true
                ,id: 'tableId'
            });

            var $ = layui.$, active = {
                reload: function(){
                    //执行重载
                    table.reload('tableId', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                            username: $('#usernameId').val(),
                            email: $('#emailId').val()
                        }
                    });
                }
            };

            $('.demoTable .layui-btn').on('click', function(){
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });


            //头工具栏事件
            table.on('toolbar(test)', function(obj){
                var checkStatus = table.checkStatus(obj.config.id);
                switch(obj.event){
                    case 'deleteAll':
                        var data = checkStatus.data;
                        layer.alert(JSON.stringify(data));
                        let ids = new Array();
                        for (let i = 0; i < data.length; i++) {
                            ids.push(data[i].id);
                        }
                        // [2,4]
                        ids = ids.join(',');// '2,4'
                        layer.confirm('真的删除行么', function(index){
                            //异步请求 同步请求
                            $.post(
                                '/user/deleteAll', //后台地址
                                {'ids': ids},    //传递数据
                                function (result) { //回调函数callback
                                    console.log(result);
                                    if (result.ok) {
                                        mylayer.okMsg(result.msg);
                                        table.reload('tableId');
                                        layer.close(index);
                                    }

                                },
                                'json'              //服务器返回json格式数据
                            );
                        });
                        break;
                    case 'add':
                        layer.open({
                            type: 2,
                            area: ['650px', '450px'],
                            content: '/user/toAdd'
                        });
                        break;

                    //自定义头工具栏右侧图标 - 提示
                    case 'LAYTABLE_TIPS':
                        layer.alert('这是工具栏右侧自定义的一个图标按钮');
                        break;
                };
            });

            //监听行工具事件
            table.on('tool(test)', function(obj){
                var data = obj.data;
                console.log(data)
                if(obj.event === 'del'){
                    layer.confirm('真的删除行么', function(index){
                        //异步请求 同步请求
                        $.post(
                            '/user/deleteById', //后台地址
                            {'id': data.id},    //传递数据
                            function (result) { //回调函数callback
                                // {'id':12,'name':'zhansgan'}
                                // [{'id':12,'name':'zhansgan'},{'id':16,'name':'lsi'}]
                                console.log(result);
                                if (result.ok) {
                                    mylayer.okMsg(result.msg);
                                    //刷新一下表格，查找最新数据
                                    table.reload('tableId');
                                    layer.close(index);
                                }

                            },
                            'json'              //服务器返回json格式数据
                        );
                    });
                } else if(obj.event === 'edit'){
                    layer.prompt({
                        formType: 2
                        ,value: data.email
                    }, function(value, index){
                        obj.update({
                            email: value
                        });
                        layer.close(index);
                    });
                }
            });
        });
    </script>

</body>
</html>